﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OpenNETCF.IoC;

namespace ProjectResistance.Shell.Entities
{
    internal class Resistor
    {
        private decimal m_resistance;
        private ToleranceValues m_tolerance;

        private const int TENS_BAND = 0;
        private const int ONES_BAND = 1;
        private const int MULTIPLIER_BAND = 2;
        private const int TOLERANCE_BAND = 3;

        public event EventHandler<GenericEventArgs<decimal>> ResistanceChanged;
        public event EventHandler<GenericEventArgs<ToleranceValues>> ToleranceChanged;

        public BandColor[] Bands { get; private set; }

        public Resistor()
        {
            // default as 0 ohm, +/- 1%
            Bands = new BandColor[]
            {
                BandColor.Black,
                BandColor.Black,
                BandColor.Black,
                BandColor.Brown
            };
        }

        public decimal Resistance 
        {
            get { return m_resistance; }
            set
            {
                if (value == m_resistance) return;

                if (value < 0) throw new ArgumentOutOfRangeException();

                m_resistance = value;
                RaiseResistanceChanged(m_resistance);
            }
        }

        public ToleranceValues Tolerance
        {
            get { return m_tolerance; }
            set
            {
                // did it actually change?
                if (value == m_tolerance) return;

                m_tolerance = value;
                RaiseToleranceChanged(m_tolerance);
            }
        }

        private void RaiseResistanceChanged(decimal resistance)
        {
            EventHandler<GenericEventArgs<decimal>> handler = ResistanceChanged;
            if (handler == null) return;

            handler(this, new GenericEventArgs<decimal>(resistance));
        }

        private void RaiseToleranceChanged(ToleranceValues tolerance)
        {
            EventHandler<GenericEventArgs<ToleranceValues>> handler = ToleranceChanged;
            if (handler == null) return;

            handler(this, new GenericEventArgs<ToleranceValues>(tolerance));
        }
    }
}
